package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Role;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.RoleService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet{
    //统一获取service对象
    private RoleService roleService=BeanFactory.getBean("roleService",RoleService.class);



    private Result delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        try {
            //1 获取请求参数,虽然只有一个id，也要封装成对象
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //2 调用service层方法，删除数据

            roleService.delete(ids);
            //3 响应结果
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取请求参数并封装
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            //2 调用service层方法，修改数据
            
            roleService.update(role);
            //3 响应结果
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            String id = request.getParameter("id");
            
            Role role=roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }


    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取请求参数并封装成Role对象
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            //2 调用service层方法添加数据
            
            roleService.save(role);

            //3 响应结果
            return new Result(true,MessageConstant.SAVE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_ROLE_FAIL);
        }

    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //获取请求参数,当前页数,每页条数
        try {
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            //2判断非空并赋默认值
            int currentPage = 1, pageSize = 5;
            if (!StringUtils.isBlank(currentPageStr)) {
                currentPage = Integer.parseInt(currentPageStr);
            }
            if (!StringUtils.isBlank(pageSizeStr)) {
                pageSize = Integer.parseInt(pageSizeStr);
            }
            //3调用service层方法，执行查询得到pageInfo
            
            PageInfo<Role> info = roleService.findByPage(currentPage, pageSize);
            //4 响应结果
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, info);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }

    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {

            List<Role> list =roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }


    }
}
